
package fileby.gui.tabs;

import com.google.inject.Inject;
import fileby.ctrl.tabs.ITabPanelListenerProvider;
import fileby.core.IDisplayableFileSource;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.awt.GridLayout;
import java.util.ArrayList;

/**
 *
 * @author Administratör
 */

public class TabPanel extends javax.swing.JPanel {

    private ArrayList<IDisplayableFileSource> sourcelist =
            new ArrayList<IDisplayableFileSource>();
    private TabFactory tabFactory;
    private IPropertyBundleProvider bundleProvider;
    private ITabPanelListenerProvider listenerProvider;


    @Inject
    public TabPanel(TabFactory tabFactory, IPropertyBundleProvider bundleProvider,
            ITabPanelListenerProvider listenerProvider) {

        this.tabFactory = tabFactory;
        this.bundleProvider = bundleProvider;
        this.listenerProvider = listenerProvider;
        initComponents();
        initCtrl();
    }

    public void addNewTab(IDisplayableFileSource source) {
        sourcelist.add(source);
        pane.addTab(source.getName(), tabFactory.createTabContent(source));
        pane.setTabComponentAt(pane.getTabCount() - 1, tabFactory.createTabComponent(source));
    }

    public void closeTab(int index) {
        pane.remove(index);
        sourcelist.remove(index);
    }

    public void updateTab(IDisplayableFileSource source) {
        if (source == getCurrentTabContent().getSource()) {
            getCurrentTabContent().updateContent();
        } else {
            ((TabContent) pane.getComponentAt(getSourceIndex(source))).setSource(source);
            ((TabComponent) pane.getTabComponentAt(getSourceIndex(source))).setSource(source);
            repaint();
        }
    }

    public void changeTab(IDisplayableFileSource oldsource, IDisplayableFileSource newsource) {
        int index = sourcelist.indexOf(oldsource);
        sourcelist.remove(oldsource);
        sourcelist.add(index, newsource);
        updateTab(newsource);
    }

    private TabContent getCurrentTabContent() {
        return (TabContent) pane.getComponentAt(pane.getSelectedIndex());
    }

    private int getSourceIndex(IDisplayableFileSource s) {
        return sourcelist.indexOf(s);
    }

    private void initCtrl() {
        pane.addChangeListener(listenerProvider.create());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pane = new javax.swing.JTabbedPane();

        PropertyBundle bundle = bundleProvider.create(this.getClass());
        pane.setBackground(bundle.getColor("pane.background")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
        setLayout(new GridLayout());
        pane.setOpaque(false);
        add(pane);
        setOpaque(false);

    }// </editor-fold>
 
    private javax.swing.JTabbedPane pane;

}
